<template>
  <div class="app-wrapper live-picture">
    <VideoPlayer :videoConfig="playerOptions"></VideoPlayer>
    <div class="live-picture-main">
      <div class="live-summary">
        <h3 class="live-summary-title">第二届世界互联网大会</h3>
        <p class="live-summary-other clearfix">239次浏览<a href="#" class="pull-right" @click.prevent="showDetailPanel = true">活动详情 ></a></p>
      </div>

      <div class="style-option-bar clearfix">
        <div class="tab-header pull-left">
          <div class="tab-header-item" 
            :class="{'active': type === 'all'}"
            @click="setType('all')"
          >全部(235)</div>
          <div class="tab-header-item"
            :class="{'active': type === 'time'}"
            @click="setType('time')"
          >时间轴</div>
        </div>
        <p class="show-style pull-right">
          <span class="item"
            @click="setOrder()"
          ><i class="album-icon" :class="order == 'desc' ? 'down' : 'up'"></i></span>
          <span class="item"
            @click="setShowMode(9)"
          ><i class="album-icon grid-nine" :class="{'active': grid == 9}"></i></span>
          <span class="item"
            @click="setShowMode(4)"
          ><i class="album-icon grid-four" :class="{'active': grid == 4}"></i></span>
          <span class="item"
            @click="setShowMode(2)"
          ><i class="album-icon grid-two" :class="{'active': grid == 1}"></i></span>
        </p>
      </div>
      <div class="live-picture-wrapper" ref="pictureWrapper">
        <template v-if="type === 'time'">
          <p class="time time-year">2019-07-12</p>
          <p class="time time-second">13:20-15:30</p>
        </template>
        <div class="live-picture-list clearfix grid-two" v-if="grid === 2">
          <div class="live-picture-item" :style="{'height': imgHeight + 'px'}"><img src="./img/live_main.jpg" alt=""></div>
          <div class="live-picture-item" :style="{'height': imgHeight + 'px'}"><img src="./img/live_main.jpg" alt=""></div>
        </div>
        <div class="live-picture-list grid-four clearfix" v-if="grid === 4">
          <div class="live-picture-item" :style="{'height': imgHeight + 'px'}"
            v-for="(item, index) in imgList"
            :key="item.id"
            :class="{'pull-left': index % 2 == 1, 'pull-left': index % 2 == 0}"
           
            @click.stop="selectedItem(item)"
          >
            <img :src="item.src" alt="">
            <span class="button-circle"
              v-show="puzzleMode != ''"
              :class="{'active': item.checked}"
            >
            </span>
          </div>
          <!-- <div class="live-picture-item" :style="{'height': imgHeight + 'px'}">
            <img src="./img/live_main.jpg" alt="">
            <span class="button-circle" @click="checkedItem"></span>
          </div> -->
        </div>
        <div class="live-picture-list grid-nine clearfix" v-if="grid === 9">
          <div class="live-picture-item" :style="{'height': imgHeight + 'px'}"><img src="./img/live_main.jpg" alt=""></div>
          <div class="live-picture-item" :style="{'height': imgHeight + 'px'}"><img src="./img/live_main.jpg" alt=""></div>
          <div class="live-picture-item" :style="{'height': imgHeight + 'px'}"><img src="./img/live_main.jpg" alt=""></div>
          <div class="live-picture-item" :style="{'height': imgHeight + 'px'}"><img src="./img/live_main.jpg" alt=""></div>
          <div class="live-picture-item" :style="{'height': imgHeight + 'px'}"><img src="./img/live_main.jpg" alt=""></div>
          <div class="live-picture-item" :style="{'height': imgHeight + 'px'}"><img src="./img/live_main.jpg" alt=""></div>
          <div class="live-picture-item" :style="{'height': imgHeight + 'px'}"><img src="./img/live_main.jpg" alt=""></div>
          <div class="live-picture-item" :style="{'height': imgHeight + 'px'}"><img src="./img/live_main.jpg" alt=""></div>
          <div class="live-picture-item" :style="{'height': imgHeight + 'px'}"><img src="./img/live_main.jpg" alt=""></div>
          <div class="live-picture-item" :style="{'height': imgHeight + 'px'}"><img src="./img/live_main.jpg" alt=""></div>
          <div class="live-picture-item" :style="{'height': imgHeight + 'px'}"><img src="./img/live_main.jpg" alt=""></div>
          <div class="live-picture-item" :style="{'height': imgHeight + 'px'}"><img src="./img/live_main.jpg" alt=""></div>
          <div class="live-picture-item" :style="{'height': imgHeight + 'px'}"><img src="./img/live_main.jpg" alt=""></div>
          <div class="live-picture-item" :style="{'height': imgHeight + 'px'}"><img src="./img/live_main.jpg" alt=""></div>
          <div class="live-picture-item" :style="{'height': imgHeight + 'px'}"><img src="./img/live_main.jpg" alt=""></div>
          <div class="live-picture-item" :style="{'height': imgHeight + 'px'}"><img src="./img/live_main.jpg" alt=""></div>
        </div>
      </div>
    </div>



    <div class="option-bar" v-show="showPopupPanel == '' && puzzleMode == ''">
      <div class="option-bar-item">
        <i class="icon icon-seek"></i><p>找我</p>
      </div>
      <div class="option-bar-item" @click="showPopupPanel = 'puzzle'">
        <i class="icon icon-puzzle"></i><p>拼图</p>
      </div>
      <div class="option-bar-item" @click="previewGallery">
        <i class="icon icon-carousel"></i><p>轮播</p>
      </div>
      <div class="option-bar-item">
        <i class="icon icon-share-3"></i><p>分享</p>
      </div>
    </div>

    <van-popup 
      position="bottom"
      overlay="false"
      overlay-class="app-masker"
      get-container=".app-wrapper"
      v-model="showDetailPanel"
    >
      <div class="panel-detail">
        <a href="#" class="panel-detail-close" @click.prevent="showDetailPanel = false">×</a>
        <p>直播名称：第二届世界互联网大会</p>
        <p>开始时间：2019-07-12 13:30</p>
        <div class="report">举报反馈</div>
      </div>
    </van-popup>


    <div class="piecing-bar"
      :class="{'show': puzzleMode != ''}"
    >
      <div class="piecing-baitem"><button class="button" @click="cancelPiecing">退出拼图</button></div>
      <div class="piecing-baitem"><span class="counts" :class="{'enabled': selectedNums > 0}" @click="piecingAction">拼图({{selectedNums}}/{{totalNums}})</span></div>
    </div>

    <PiecingPage
      v-if="isShowPiecingPage"
      :liveInfomation="info"
      :puzzleMode="puzzleMode"
      @cancel-piecing="cancelPiecing"
    ></PiecingPage>

  <div class="puzzle-masker"
    v-if="showPopupPanel == 'puzzle'"
    @click="showPopupPanel = ''"
  ></div>
  <div class="puzzle-panel panel" :class="{'show': showPopupPanel == 'puzzle'}">
    <p class="panel-title">请选择拼图样式</p>
    <div class="puzzle-panel-body panel-body">
      <div class="puzzle-item" @click="setPuzzleMode('photo')">
        <div class="img-box">
          <img src="./img/pingtu_pj.png" alt="">
        </div>
        <p class="title">单张</p>
      </div>
      <div class="puzzle-item" @click="setPuzzleMode('list')">
        <div class="img-box">
          <img src="./img/pingtu_pj.png" alt="">
        </div>
        <p class="title">拼接</p>
      </div>
      <div class="puzzle-item" @click="setPuzzleMode('grid')">
        <div class="img-box">
          <img src="./img/pingtu_gg.png" alt="">
        </div>
        <p class="title">宫格</p>
      </div>
      <div class="puzzle-item">
        <div class="img-box" @click="setPuzzleMode('gif')">
          <img src="./img/pingtu_gif.png" alt="">
        </div>
        <p class="title">动图</p>
      </div>
    </div>
  </div>

  </div>
</template>

<script>
import VideoPlayer from '@/components/Live/VideoPlayer2.vue';
import PiecingPage from './Piecing.vue';
import {ImagePreview} from 'vant';

let imgUrl = require('./img/live_main.jpg');
export default {
  components: {
    VideoPlayer,
    PiecingPage
  },
  props: {},
  data() {
    return {
      type: 'all',  // 全部还是时间轴
      limits: 48,   // 图片显示数量
      order: 'desc',  //asc 升降序
      grid: 4,        // 2 4 or 9 grid
      info:{
        title: '第三届世界软件开发大会',
        imgList: [imgUrl]
      },
      imgHeight: 'auto',
      isShowPiecingPage: false, // 是否显示拼接页面
      puzzleMode: '', // 拼图模式  gif grid list
      showPopupPanel: '',  // 是否显示选择拼装格式面板
      showDetailPanel: false, // 是否显示详情面板
      selectedNums: 0,
      imgList: [
        { id: 1, src: imgUrl, checked: false },
        { id: 2, src: imgUrl, checked: false },
        { id: 3, src: imgUrl, checked: false },
        { id: 4, src: imgUrl, checked: false },
        { id: 5, src: imgUrl, checked: false },
        { id: 6, src: imgUrl, checked: false },
        { id: 7, src: imgUrl, checked: false },
        { id: 8, src: imgUrl, checked: false }
      ],
      playerOptions: {
        sources: [{
          type: "application/x-mpegURL",
          src: liveUrl
        }],
        poster: require("@/assets/img/home_banner.jpg")
      }
    };
  },
  computed: {
    totalNums(){
      if(this.puzzleMode === 'photo') return 1;
      if(this.imgList.length > 10) return 10;
      return this.imgList.length;
    },
    selectedNums(){
      return this.imgList.filter((item)=>item.checked).length;
    },
    imgHeight(){
      let gridMap = {'2': 2, '4': 2, '9': 3}; 
      console.log(';here')
      let wrapperHeight = this.$refs.pictureWrapper.offsetHeight;
      return wrapperHeight / gridMap[this.grid];
    }
  },
  methods: {
    resetImgHeight(){
      this.$nextTick(()=>{
        let gridMap = {'2': 2, '4': 2, '9': 3}; 
        console.log(';here')
        let wrapperHeight = this.$refs.pictureWrapper.offsetHeight;
        this.imgHeight = wrapperHeight / gridMap[this.grid] + 'px';;
      });
    },
    setOrder(order){
      this.order = this.order == 'desc' ? 'asc' : 'desc';
    },
    setShowMode(grid){
      this.grid = grid;
    },
    setType(type){
      this.type = type;
    },
    previewGallery(){
        const instance = ImagePreview({
            images: ['https://img.yzcdn.cn/vant/apple-3.jpg', 'https://img.yzcdn.cn/vant/apple-2.jpg', 'https://img.yzcdn.cn/vant/apple-4.jpg'],
        });
    },
    // 选择图片
    selectedItem(item){
      // 如果在拼图模式下，则为选择
      // 如果在普通模式下，则为预览
      if(this.puzzleMode != ''){
        item.checked = !item.checked;
        this.selectedNums = this.imgList.filter((item)=>item.checked).length;
      }else{
        let showUrl = 'https://img.yzcdn.cn/vant/apple-3.jpg';
        const instance = ImagePreview({
            images: [showUrl],
        });
      }
    },
    cancelPiecing(){
      this.imgList.forEach((item)=>{
        item.checked = false;
      });
      this.selectedNums = 0;
      this.puzzleMode = '';
      this.isShowPiecingPage = false;
    },
    setPuzzleMode(mode){
      this.puzzleMode = mode;
      this.showPopupPanel = '';
    },
    piecingAction(){
      if(!this.selectedNums) return this.$toast('请选择你的图片');
      if(this.puzzleMode === 'photo'){
        if(this.selectedNums > 1) return this.$toast('图片数量最多1张')
        this.isShowPiecingPage = true;
        this.info.imgList = this.imgList.filter((item)=>item.checked).map(item=>imgUrl);
        return;
      }
      if(this.selectedNums % 2 > 0) return this.$toast('图片数量不能为奇数');
      // 数量 > 0 && 数量为奇数
      this.isShowPiecingPage = true;
      this.info.imgList = this.imgList.filter((item)=>item.checked).map(item=>imgUrl);
    }
  },
  created() {},
  mounted() {
    this.$nextTick(()=>{
      this.resetImgHeight();
    });
    
  }
};
</script>
<style lang="scss" scoped>
@import '@/assets/config.scss';

.live-picture-main {
  height: calc(100% - 350px);
  padding: 0 35px;
  background-color: #fff;
}

.live-summary {
  padding: 16px 0;
  border-bottom: 1px solid #f0f0f0;
}

.live-summary-title {
  font-size: 34px;
  color: #000;
}

.live-summary-other {
  font-size: 24px;
  line-height: 2;
  color: #999;
  > a{
    color: $blue;
  }
}


.style-option-bar {
  line-height: 90px;
  .tab-header-item {
    float: left;
    margin-right: 90px;
    font-size: 30px;
    color: #333;

    &.active{
      position: relative;
      color: $blue;
      &:after{
        position: absolute;
        content: '';
        width: 100%;
        height: 4px;
        background-color: $blue;
        bottom: 0;
        left: 0;
      }
    }
  }

  .show-style {
    font-size: 0;
    .item {
      display: inline-block;
      width: 40px;
      height: 40px;
      margin-left: 30px;
      font-size: 36px;
      line-height: 40px;
      vertical-align: middle;
      &.active { color: $blue; }
      &:first-child { margin-left: 0; }
    }
  }
}

.live-picture-wrapper {
  height: calc(100% - 132px - 90px);
  padding-top: 20px;
  overflow: hidden;
  > .time { line-height: 1.5; }
  .time-year {
    font-size: 30px;
    color: #333;
  }

  .time-second {
    font-size: 24px;
    color:#999;
  }
}

.live-picture-list {
  padding-top: 10px;

  .live-picture-item {
    position: relative; 
    img { 
      width: 100%;
      height: 100%;
      object-fit: cover;
    } 

    .button-circle {
      position: absolute;
      top: 16px;
      right: 16px;
      border-radius: 100%;
    }
  }




  &.grid-four {
    .live-picture-item {
      float: left;
      width: 50%;
      margin-bottom: 4px;
      &:first-child,&:nth-of-type(3n){ padding-right: 2px; }
      &:nth-of-type(2n){ padding-left: 2px; }
    }
  }

  &.grid-nine {
    .live-picture-item {
      float: left;
      width: calc(100% / 3);
      margin-bottom: 4px;
      padding: 0 2px;
      // &:first-child,&:nth-of-type(3n){  }
      // &:nth-of-type(3n){ padding-left: 2px; }
    }
  }
}

.option-bar {
  position: fixed;
  right: 35px;
  bottom: 24px;
  font-size: 24px;
  color:#fff;
  .icon {
    display: inline-block;
    margin-top: 6px;
    font-size: 42px;
  }
  .option-bar-item {
    width: 94px;
    height: 94px;
    margin-bottom: 24px;

    text-align: center;
    border-radius: 100%;
    background-color: rgba(21, 73, 235, .7);
    p {
      font-size: 24px;
    }
  }
}


.panel-detail {
  height: 495px;
  padding: 60px 35px 40px;
  background-color: #fff;
  line-height: 2;

  .panel-detail-close {
    position: absolute;
    top: 20px;
    right: 20px;
    font-size: 64px;
    color: #666;
    line-height: 1;
  }

  p {
    font-size: 30px;
    color: #000;
  }

  .report {
    font-size: 28px;
    color: $blue;
    position: absolute;
    left: 50%;
    bottom: 40px;
    transform: translate(-50%, 0);
  }
}

.piecing-bar {
  display: flex;
  width: 100%;
  padding: 16px 0 14px;
  border-top: 1px solid #f0f0f0;
  position: fixed;
  left: 0;
  bottom: 0;
  background: #fff;
  transition: all ease-in-out .15s;
  transform: translate3d(0, 100%, 0);
  &.show {
    transform: translate3d(0, 0, 0);
  }

  >.piecing-baitem {
    flex: 1;
    text-align: center;
    font-size: 0;
  }

  .button, .counts {
    display: inline-block;
    width: 268px;
    height: 80px;
    font-size: 32px;
    line-height: 80px;
    border-radius: 40px;
  }

  .button {
    color: #a1710d;
    background-color: $color-orange;
  }

  .counts {
    color: #747474;
    background-color: #ededed;
    &.enabled {
      color: #fff;
      background-color: rgba(21, 73, 235, .7);
    }
  }
}



.choicePt-mask {
    position: fixed;
    top: 0;
    left: 50%;
    right: 0;
    bottom: 0;
    width: 100%;
    min-width: 320px;
    max-width: 480px;
    background: rgba(0,0,0,0.3);
    transform: translateX(-50%);
    -webkit-transform: translateX(-50%);
    z-index: 500;
}

.puzzle-masker {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background-color: rgba(0, 0, 0, .5);
}

.puzzle-panel  {
  width: 100%;
  padding: 50px;
  position: fixed;
  bottom: 0;
  z-index: 60;
  transform: translate3d(0, 100%, 0);
  border-radius: 20px 20px 0 0;
  background: #fff;
  text-align: center;
  transition: all linear .3s;
  &.show{
    transform: translate3d(0, 0, 0);
  }
  .panel-title {
    margin-bottom: 50px;
    font-size: 32px;
    color: #353535;
    font-weight: bold;
    line-height: 1;
  }

  .puzzle-panel-body {
    display: flex;
    justify-content: space-between;
  }

  .puzzle-item {
    flex: 1;
    border-radius: 4px;
  }

  .title {
    margin-top: 10px;
    font-size: 28px;
    color: #353535;
    line-height: 48px;
  }

  .img-box {
    width: 100px;
    height: 100px;
    margin: 0 auto;
    > img { width: 100%; height: 100%; }
  }
}

</style>
